package com.niubility.server.Impl;


import com.niubility.dao.ArticleDao;
import com.niubility.dao.DepartmentDao;
import com.niubility.dao.UsersDao;
import com.niubility.entity.Article;
import com.niubility.entity.UsersCollection;
import com.niubility.server.IArticleServer;
import com.niubility.server.IFollowServer;
import com.niubility.server.IMessageServer;
import com.niubility.server.IUsersCollectionServer;
import com.niubility.vo.ArticleAll;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
/**
 * @ClassName: ArticleDao
 * @Description: 实现文章各个部分的的显示,以及用户对自己文章的管理
 * @author: 张晨
 * @DATE: 2018/2/2
 */
@Service("articleServer")
public class ArticleServer implements IArticleServer{

    @Resource
    private ArticleDao articleDao;

    @Resource
    private UsersDao usersDao;

    @Resource
    private DepartmentDao departmentDao;

    @Resource
    private IMessageServer messageServer;

    @Resource
    IUsersCollectionServer iUsersCollectionServer;

    @Resource
    IFollowServer followServer;


//*************************************************************************************
    public int SelectArticleNum(){
        int page=0;
        int num=articleDao.SelectArticleNum();
        if(num%10!=0){
            page=num/10+1;
        }
        else{
            page=num/10;
        }
        return page;
    }

    public boolean UpdateGoodnum(int id,int num){
        return articleDao.UpdateGoodnum(id,num)>0?true:false;
    }

    public boolean UpdateReadNum(int artId){
        return articleDao.UpdateReadNum(artId)>0?true:false;
    }

    //用户可以管理删除自己的文章
    public boolean Delete(int id,int userId) {
        return articleDao.Delete(id,userId)>0?true:false;
    }

    //通过文章ID查看具体文章内容
    public ArticleAll SelectById(int id) {
        List<ArticleAll> articleAllList = SelectByRecommend(0,0,id,0,0,4,null);
        if(articleAllList.size()!=0){
            return articleAllList.get(0);
        }else{
            return null;
        }
    }

    //通过用户ID查看自己发布的文章
//    public List<ArticleAll> SelectByMasterid(int id,int page,int num) {
//        List<Article> articleList = articleDao.SelectByMasterid(id, (page - 1) * num, num);
//        List<ArticleAll> articleVoList = new ArrayList<ArticleAll>();
//        if (id > 99999999 && id < 200000000) {
//            for (Article article : articleList
//                    ) {
//                ArticleAll articleAll = new ArticleAll();
//                articleAll.setArtAssessnum(article.getArtAssessnum());
//                articleAll.setArtCollectnum(article.getArtCollectnum());
//                articleAll.setArtContext(article.getArtContext());
//                articleAll.setArtCover(article.getArtCover());
//                articleAll.setArtDate(article.getArtDate());
//                articleAll.setArtGoodNum(article.getArtGoodNum());
//                articleAll.setArtId(article.getArtId());
//                articleAll.setArtReadnum(article.getArtReadnum());
//                articleAll.setArtTexthead(article.getArtTexthead());
//                articleAll.setArtTitle(article.getArtTitle());
//                articleAll.setAuthorName(usersDao.LoadById(article.getArtMasterId()).getUserName());
//                articleAll.setAuthorPicture(usersDao.LoadById(article.getArtMasterId()).getUserPicture());
//                articleVoList.add(articleAll);
//
//            }
//        }
//        if(id>199999999&&id<300000000){
//            for (Article article : articleList
//                    ) {
//                ArticleAll articleAll = new ArticleAll();
//                articleAll.setArtAssessnum(article.getArtAssessnum());
//                articleAll.setArtCollectnum(article.getArtCollectnum());
//                articleAll.setArtContext(article.getArtContext());
//                articleAll.setArtCover(article.getArtCover());
//                articleAll.setArtDate(article.getArtDate());
//                articleAll.setArtGoodNum(article.getArtGoodNum());
//                articleAll.setArtId(article.getArtId());
//                articleAll.setArtReadnum(article.getArtReadnum());
//                articleAll.setArtTexthead(article.getArtTexthead());
//                articleAll.setArtTitle(article.getArtTitle());
//                articleAll.setAuthorName(departmentDao.loadDepById(article.getArtMasterId()).getDepName());
//                articleAll.setAuthorPicture(departmentDao.loadDepById(article.getArtMasterId()).getDepPicture());
//                articleVoList.add(articleAll);
//
//            }
//        }
//        //System.out.println(id);
//        //System.out.println(articleVoList.size());
//        return articleVoList;
//    }


    //点击推荐所要显示的文章
    public List<ArticleAll> SelectByRecommend(int page,int displayCounts,int artId,int userId,int masterId,int tag,int[] foledId){
        int index=(page-1)*displayCounts;
        List<Article> articleList=null;
        if(tag==1)          //推荐区域显示的文章
            articleList= articleDao.SelectByRecommend(index,displayCounts);
        if(tag==2)         //热门区域显示的文章
            articleList=articleDao.SelectByHot(index,displayCounts);
        if(tag==3)        //关注区域显示的文章
            articleList=articleDao.SelectByFollow(foledId,index,displayCounts);
        if(tag==4)       //查看文章具体内容
            articleList = articleDao.SelectById(artId);
        if(tag==5)       //通过作者ID查看文章
            articleList = articleDao.SelectByMasterid(masterId,index,displayCounts);
        if(tag==6)        //王勇调用
            articleList=articleDao.CallSelectById(foledId,index,displayCounts);
        List<ArticleAll> articleAllList = new LinkedList<ArticleAll>();
        UsersCollection usersCollection = new UsersCollection();
        for (Article a:articleList){
            ArticleAll articleAll = new ArticleAll();
            ////System.out.println(article.toString());

            articleAll.setArtAssessnum(a.getArtAssessnum());
            articleAll.setArtCollectnum(a.getArtCollectnum());
            articleAll.setArtContext(a.getArtContext());
            articleAll.setArtCover(a.getArtCover());
            articleAll.setArtDate(a.getArtDate());
            articleAll.setArtGoodNum(a.getArtGoodNum());
            articleAll.setArtId(a.getArtId());
            articleAll.setArtReadnum(a.getArtReadnum());
            articleAll.setArtTexthead(a.getArtTexthead());
            articleAll.setArtTitle(a.getArtTitle());
            articleAll.setAuthorId(a.getArtMasterId());
            articleAll.setGoodMark(messageServer.SelectAssessMark(a.getArtMasterId(),userId,a.getArtId()));//点赞标志
            usersCollection.setArtId(a.getArtId());
            usersCollection.setUsersId(userId);
            articleAll.setCollectMark(iUsersCollectionServer.LoadisColl(usersCollection));  //收藏标志
            if(a.getArtMasterId()>199999999&&a.getArtMasterId()<300000000){
                if(departmentDao.loadDepById(a.getArtMasterId())!=null) {
                    articleAll.setAuthorName(departmentDao.loadDepById(a.getArtMasterId()).getDepName());
                    articleAll.setAuthorPicture(departmentDao.loadDepById(a.getArtMasterId()).getDepPicture());
                }
            }
            if(a.getArtMasterId()>99999999&&a.getArtMasterId()<200000000){

                if(usersDao.LoadById(a.getArtMasterId())!=null) {
                    articleAll.setAuthorName(usersDao.LoadById(a.getArtMasterId()).getUserName());
                    articleAll.setAuthorPicture(usersDao.LoadById(a.getArtMasterId()).getUserPicture());
            }
            }
            articleAllList.add(articleAll);
        }
        return articleAllList;
    }


    //文章的收藏数量的更改        张家乐调用方法
    public boolean UpdateCollectNum(int artId,int num){
        return articleDao.UpdateCollectNum(artId,num)>0?true:false;
    }

    //保存文章的方法           徐松鹤调用保存文章的发法
    public boolean Save(Article article){
        return articleDao.Save(article)>0?true:false;
    }

    //调用查询文章接口          王勇调用方法
//    public List<ArticleAll> CallSelectById(int[] arry,int index,int displaycount){
//        return articleDao.CallSelectById(arry,index,displaycount);
//
//    }

    //根据文章题目和发布者ID查     徐松鹤调用方法
    public int SelectByTitle(Article article){
        return articleDao.SelectByTitle(article);
    }

    //徐松鹤更改文章评论数量    徐松鹤调用方法
    public boolean UpdateAssessnum(Article article){
        return articleDao.UpdateAssessnum(article)>0?true:false;
    }

    //根据发布者ID查询发布的文章数量
    public int articleNum(int id){
        return articleDao.articleNum(id);
    }
}
